﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAO;

namespace Sklad.WindowsApplication
{
    public partial class AddProduct : Form
    {
        DaoRepository dao;
        public AddProduct()
        {
            dao = new DaoRepository();
            InitializeComponent();
            //drop_category.Items.AddRange(dao.CategoryRepository.GetAll().ToArray());
            BindingSource bs = new BindingSource();
            var temp = dao.CategoryRepository.GetAll().ToList<Category>();
            if (temp.Count == 0)
                return;
            bs.DataSource = temp;

            drop_category.DataSource = bs;
            drop_category.DisplayMember = "Name";
           
            drop_category.ValueMember = "Id";
            drop_category.SelectedIndex = 0;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            Product prod = new Product();
            prod.Name = txt_name.Text;
            prod.Description = txt_description.Text;
            if (drop_category.SelectedValue == null)
            {
                MessageBox.Show("Выберите категорию");
                return;
            }
            prod.Id_Category = Int32.Parse(drop_category.SelectedValue.ToString());
            dao.ProductRepository.Add(prod);
            int id = prod.Id;
            MainEmployeeForm.bg();
            this.Dispose();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
